﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BDH.PRE.Form
{
    using BDH.DAL.Entities;
    public partial class FrmPhong : BDH.PRE.Form.FrmInput
    {
        BLL.PhongBLL _bll = new BLL.PhongBLL();
        public int id_nhatro;
        public FrmPhong()
        {
            InitializeComponent();

            SetCaptionForm("Danh sách Phòng");
            btnSelect.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            btnPrint.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            ClearDataBindings();
            ReadOnlyControl(false);
            ChangeStatus(false);
            ResetText();
            txtTenphong.Focus();

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var _id = Convert.ToInt32(gvPhong.GetFocusedRowCellValue("id"));

            DialogResult result;
            var ok = _bll.Delete(_id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                    MessageBox.Show("Xóa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txtTenphong.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var ten = Proper(txtTenphong.Text);

            if (IsAdd)
            {
                var o = new Phong()
                {
                    tenphong = ten,
                    soluong = Convert.ToInt32("0" + txtSoluong.EditValue),
                    gia = Convert.ToDecimal("0" + txtSoluong.EditValue)
                };

                var oki = _bll.Insert(o);
                if (oki)
                {
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                var _id = Convert.ToInt32(gvPhong.GetFocusedRowCellValue("id"));

                var o = new Phong()
                {
                    id = _id,
                    tenphong = ten,
                    soluong = Convert.ToInt32("0" + txtSoluong.EditValue),
                    gia = Convert.ToDecimal("0" + txtSoluong.EditValue)
                };

                var oki = _bll.Update(o);
                if (oki)
                {
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txtTenphong.EditValue = null;
            txtGia.EditValue = null;
            txtSoluong.EditValue = null;

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtTenphong.DataBindings.Clear();
            txtGia.DataBindings.Clear();
            txtSoluong.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtTenphong.DataBindings.Add("EditValue", _dtb, ".tenphong");
            txtGia.DataBindings.Add("EditValue", _dtb, ".gia");
            txtSoluong.DataBindings.Add("EditValue", _dtb, ".soluong");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {

            txtTenphong.Properties.ReadOnly = isReadOnly;
            txtGia.Properties.ReadOnly = isReadOnly;
            txtSoluong.Properties.ReadOnly = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _bll.Select();

            if (_dtb != null)
                dgPhong.DataSource = _dtb;

            base.LoadData();
        }
        #endregion

        


    }
}
